package com.boss.salary.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.boss.asset.service.IAssetInfoService;
import com.boss.common.entity.WfActionDTO;
import com.boss.common.enums.StatusCodeEnum;
import com.boss.common.service.ICommonService;
import com.boss.salary.constant.ApiElementConstant;
import com.boss.salary.dao.*;

import com.boss.salary.entity.*;
import com.boss.salary.service.IAgencyInfoService;
import com.boss.salary.service.IHomeService;
import com.boss.salary.service.ISalaryBackLogService;

import com.boss.salary.workflow.service.ISalaryWorkFlowService;
import com.boss.sys.ca.user.dto.UserDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <br>Title:
 * <br>Description:
 * <br>Author:
 * <br>Date:
 */
@Service
public class HomeServiceImpl implements IHomeService {

    @Autowired
    private ISalaryWorkFlowService iSalaryWorkFlowService;

    @Autowired
    private IAgencyInfoAuditDao iAgencyInfoAuditDao;

    @Autowired
    private IPersonOperateDao iPersonOperateDao;

    @Autowired
    private IAgencyVerifyDao iAgencyVerifyDao;

    @Autowired
    private ICommonService iCommonService;

    @Autowired
    private IAgencyInfoService iAgencyInfoService;

    @Autowired
    private IAssetInfoService iAssetInfoService;

    @Autowired
    private ISalaryBackLogService salaryBackLogService;
    @Autowired
    private ISalaryBankPayBackDao iSalaryBankPayBackDao;
    @Autowired
    private IPersonExtSubmitDao personExtSubmitDao;

    @Override
    public List<Map<String, Object>> getAuditCount(UserDTO userDTO, List<Map<String, Object>> menuParam) {
        List<Map<String, Object>> result = new ArrayList<>();
        //定义一个单位id对应单位code+name得map
        Map<String, String> agencyMap = new HashMap<>();
        //定义一个单位bizkey对应单位code+name的map
        Map<String, String> avMap = new HashMap<>();
        //获取全部单位信息
        List<AgencyInfo> agencyInfos = iAgencyInfoService.getAllAgency(userDTO);
        for (AgencyInfo agency : agencyInfos) {
            //把所有单位信息放到map里方便后面取值
            agencyMap.put(agency.getId(), agency.getAgencyCode() + agency.getAgencyName());
        }
        // 获取有权限的单位数据
        JSONArray jsonArray = iAssetInfoService.getAuthAgency(userDTO, ApiElementConstant.AGENCY);
        //获取全部单位信息
        List<String> agencyIds = iAgencyInfoService.getSendSalaryAgencyId(jsonArray, userDTO);
        //获取所有代编单位状态
        List<AgencyVerify> avs = iAgencyVerifyDao.getSalaryDataByAgencyId(agencyIds);
        for (AgencyVerify av : avs) {
            //把所有单位信息放到map里方便后面取值
            avMap.put(av.getBizKey(), agencyMap.get(av.getAgencyId()));
        }

        //查询当前单位AgencyVerify表，判断待办事项是否为本单位待办用
//        AgencyVerify av = iAgencyVerifyDao.getInfoByAgencyId(userDTO.getAgency());
        if (!StringUtils.isEmpty(menuParam) && menuParam.size() > 0) {
            Map<String, Object> auditMap = null;
            for (Map<String, Object> map : menuParam) {
                // 单位审核
                if ("unitAudit".equals(map.get("menuIden"))) {
                    List<String> childrenId = iCommonService.getAgencyChildrenId(userDTO, "0");
                    Map<String, Object> queryParam = new HashMap<>(1);
                    queryParam.put("flag", "1");
                    int total = iAgencyInfoAuditDao.queryAgencyInfo(queryParam, childrenId);
                    if (total > 0) {
                        auditMap = new HashMap<>(2);
                        auditMap.put("businessType", map.get("menuName"));
                        auditMap.put("dataCount", total);
                        auditMap.put("guid", map.get("guid"));
                        result.add(auditMap);
                    }
                }// 人员变动审核
               /* else if ("personEdit".equals(map.get("menuIden"))) {
                    Map<String, Object> queryParam = new HashMap<>(1);
                    queryParam.put("agencyId", userDTO.getAgency());
                    int total = iPersonOperateDao.getPersonWaitInPersonTotal(queryParam);
                    if (total > 0) {
                        auditMap = new HashMap<>(2);
                        auditMap.put("businessType", "待调入人员");
                        auditMap.put("dataCount", total);
                        auditMap.put("guid", map.get("guid"));
                        result.add(auditMap);
                    }
                }
                // 人员变动审核
                else if ("personAudit".equals(map.get("menuIden"))) {
                    Map<String, Object> queryParam = new HashMap<>(1);
                    queryParam.put("flag", "1");
                    queryParam.put("agencyId", userDTO.getAgency());
                    int total = iPersonOperateDao.getPersonInfoChangeOperateTotal(queryParam);
                    if (total > 0) {
                        auditMap = new HashMap<>(2);
                        auditMap.put("businessType", map.get("menuName"));
                        auditMap.put("dataCount", total);
                        auditMap.put("guid", map.get("guid"));
                        result.add(auditMap);
                    }
                }// 工资单位编报*/
                else if ("salaryEdit".equals(map.get("menuIden"))) {
                    WfActionDTO wfActionDTO = new WfActionDTO();
                    // 菜单id
                    wfActionDTO.setMenuguid(map.get("guid").toString());
                    wfActionDTO.setStatus(StatusCodeEnum.BESENDBACK.getCode());
                    List<String> bizKeys = iSalaryWorkFlowService.queryWorkflowInfo(userDTO, wfActionDTO);
                    int total = 0;
//                    if (!StringUtils.isEmpty(bizKeys) && bizKeys.size() > 0) {
//                        total = bizKeys.size();
//                    }
                    if (!StringUtils.isEmpty(bizKeys) && bizKeys.size() > 0) {
                        for (String bizKey : bizKeys) {
                            if (StringUtils.isEmpty(avMap.get(bizKey))) {
                                continue;
                            }
                            auditMap = new HashMap<>(2);
                            auditMap.put("businessType", avMap.get(bizKey) + "工资数据被退回");
                            auditMap.put("dataCount", 1);
                            auditMap.put("guid", map.get("guid"));
                            auditMap.put("isback", 1);
                            result.add(auditMap);
                        }
                    }
                }
                // 工资单位审核
                else if ("salaryAudit".equals(map.get("menuIden"))) {
                    WfActionDTO wfActionDTO = new WfActionDTO();
                    // 菜单id
                    wfActionDTO.setMenuguid(map.get("guid").toString());
                    wfActionDTO.setStatus(StatusCodeEnum.TODO.getCode());
                    List<String> bizKeys = iSalaryWorkFlowService.queryWorkflowInfo(userDTO, wfActionDTO);
                    if (!StringUtils.isEmpty(bizKeys) && bizKeys.size() > 0) {
                        for (String bizKey : bizKeys) {
                            if (StringUtils.isEmpty(avMap.get(bizKey))) {
                                continue;
                            }
                            auditMap = new HashMap<>(2);
                            auditMap.put("businessType", avMap.get(bizKey) + map.get("menuName"));
                            auditMap.put("dataCount", 1);
                            auditMap.put("guid", map.get("guid"));
                            auditMap.put("isback", 0);
                            result.add(auditMap);
                        }
                    }
                    wfActionDTO.setStatus(StatusCodeEnum.BESENDBACK.getCode());
                    List<String> bizKeybacks = iSalaryWorkFlowService.queryWorkflowInfo(userDTO, wfActionDTO);
                    if (!StringUtils.isEmpty(bizKeybacks) && bizKeybacks.size() > 0) {
                        for (String bizKey : bizKeybacks) {
                            if (StringUtils.isEmpty(avMap.get(bizKey))) {
                                continue;
                            }
                            auditMap = new HashMap<>(2);
                            auditMap.put("businessType", avMap.get(bizKey) + "工资数据被退回");
                            auditMap.put("dataCount", 1);
                            auditMap.put("guid", map.get("guid"));
                            auditMap.put("isback", 1);
                            result.add(auditMap);
                        }
                    }
                } else if ("salaryAuditMiddle".equals(map.get("menuIden"))) {
                    WfActionDTO wfActionDTO = new WfActionDTO();
                    // 菜单id
                    wfActionDTO.setMenuguid(map.get("guid").toString());
                    wfActionDTO.setStatus(StatusCodeEnum.TODO.getCode());
                    List<String> bizKeys = iSalaryWorkFlowService.queryWorkflowInfo(userDTO, wfActionDTO);
                    if (!StringUtils.isEmpty(bizKeys) && bizKeys.size() > 0) {
                        for (String bizKey : bizKeys) {
                            if (StringUtils.isEmpty(avMap.get(bizKey))) {
                                continue;
                            }
                            auditMap = new HashMap<>(2);
                            auditMap.put("businessType", avMap.get(bizKey) + map.get("menuName"));
                            auditMap.put("dataCount", 1);
                            auditMap.put("guid", map.get("guid"));
                            auditMap.put("isback", 0);
                            result.add(auditMap);
                        }
                    }
                    wfActionDTO.setStatus(StatusCodeEnum.BESENDBACK.getCode());
                    List<String> bizKeybacks = iSalaryWorkFlowService.queryWorkflowInfo(userDTO, wfActionDTO);
                    if (!StringUtils.isEmpty(bizKeybacks) && bizKeybacks.size() > 0) {
                        for (String bizKey : bizKeybacks) {
                            if (StringUtils.isEmpty(avMap.get(bizKey))) {
                                continue;
                            }
                            auditMap = new HashMap<>(2);
                            auditMap.put("businessType", avMap.get(bizKey) + "工资数据被退回");
                            auditMap.put("dataCount", 1);
                            auditMap.put("guid", map.get("guid"));
                            auditMap.put("isback", 1);
                            result.add(auditMap);
                        }
                    }
                }
                // 工资财政确认
                else if ("salaryConfirm".equals(map.get("menuIden"))) {
                    WfActionDTO wfActionDTO = new WfActionDTO();
                    // 菜单id
                    wfActionDTO.setMenuguid(map.get("guid").toString());
                    wfActionDTO.setStatus(StatusCodeEnum.TODO_BESENDBACK.getCode());
                    List<String> bizKeys = iSalaryWorkFlowService.queryWorkflowInfo(userDTO, wfActionDTO);
                    int total = 0;
                    if(!StringUtils.isEmpty(bizKeys) && bizKeys.size()>0) {
                        for (String bizKey : bizKeys) {
                            if (!StringUtils.isEmpty(avMap.get(bizKey))) {
                                total++;
                            }
                        }
                    }
                    if (total > 0) {
                        auditMap = new HashMap<>(2);
                        auditMap.put("businessType", map.get("menuName"));
                        auditMap.put("dataCount", total);
                        auditMap.put("guid", map.get("guid"));
                        result.add(auditMap);
                    }
                }
                // 初审
                else if ("firstAudit".equals(map.get("menuIden"))) {
                    WfActionDTO wfActionDTO = new WfActionDTO();
                    // 菜单id
                    wfActionDTO.setMenuguid(map.get("guid").toString());
                    wfActionDTO.setStatus(StatusCodeEnum.TODO.getCode());
                    List<String> bizKeys = iSalaryWorkFlowService.queryWorkflowInfo(userDTO, wfActionDTO);
                    int total = 0;
                    if(!StringUtils.isEmpty(bizKeys) && bizKeys.size()>0) {
                        for (String bizKey : bizKeys) {
                            if (!StringUtils.isEmpty(avMap.get(bizKey))) {
                                total++;
                            }
                        }
                    }
                    if (total > 0) {
                        auditMap = new HashMap<>(2);
                        auditMap.put("businessType", map.get("menuName"));
                        auditMap.put("dataCount", total);
                        auditMap.put("guid", map.get("guid"));
                        result.add(auditMap);
                    }

                    wfActionDTO.setStatus(StatusCodeEnum.BESENDBACK.getCode());
                    List<String> bacbizKeys = iSalaryWorkFlowService.queryWorkflowInfo(userDTO, wfActionDTO);
                    int bactotal = 0;
                    if(!StringUtils.isEmpty(bacbizKeys) && bacbizKeys.size()>0) {
                        for (String bizKey : bacbizKeys) {
                            if (!StringUtils.isEmpty(avMap.get(bizKey))) {
                                bactotal++;
                            }
                        }
                    }
                    if (bactotal > 0) {
                        auditMap = new HashMap<>(2);
                        auditMap.put("businessType", map.get("menuName") + "存在被退回信息");
                        auditMap.put("dataCount", bactotal);
                        auditMap.put("guid", map.get("guid"));
                        auditMap.put("isback", 1);
                        result.add(auditMap);
                    }
                }
                // 复审
                else if ("secondAudit".equals(map.get("menuIden"))) {
                    WfActionDTO wfActionDTO = new WfActionDTO();
                    // 菜单id
                    wfActionDTO.setMenuguid(map.get("guid").toString());
                    wfActionDTO.setStatus(StatusCodeEnum.TODO.getCode());
                    List<String> bizKeys = iSalaryWorkFlowService.queryWorkflowInfo(userDTO, wfActionDTO);
                    int total = 0;
                    if(!StringUtils.isEmpty(bizKeys) && bizKeys.size()>0) {
                        for (String bizKey : bizKeys) {
                            if (!StringUtils.isEmpty(avMap.get(bizKey))) {
                                total++;
                            }
                        }
                    }
                    if (total > 0) {
                        auditMap = new HashMap<>(2);
                        auditMap.put("businessType", map.get("menuName"));
                        auditMap.put("dataCount", total);
                        auditMap.put("guid", map.get("guid"));
                        result.add(auditMap);
                    }

                    wfActionDTO.setStatus(StatusCodeEnum.BESENDBACK.getCode());
                    List<String> bacbizKeys = iSalaryWorkFlowService.queryWorkflowInfo(userDTO, wfActionDTO);
                    int bactotal = 0;
                    if(!StringUtils.isEmpty(bacbizKeys) && bacbizKeys.size()>0) {
                        for (String bizKey : bacbizKeys) {
                            if (!StringUtils.isEmpty(avMap.get(bizKey))) {
                                bactotal++;
                            }
                        }
                    }
                    if (bactotal > 0) {
                        auditMap = new HashMap<>(2);
                        auditMap.put("businessType", map.get("menuName") + "存在被退回信息");
                        auditMap.put("dataCount", bactotal);
                        auditMap.put("guid", map.get("guid"));
                        auditMap.put("isback", 1);
                        result.add(auditMap);
                    }

                }
                // 终审
                else if ("finalAudit".equals(map.get("menuIden"))) {
                    WfActionDTO wfActionDTO = new WfActionDTO();
                    // 菜单id
                    wfActionDTO.setMenuguid(map.get("guid").toString());
                    wfActionDTO.setStatus(StatusCodeEnum.TODO_BESENDBACK.getCode());
                    List<String> bizKeys = iSalaryWorkFlowService.queryWorkflowInfo(userDTO, wfActionDTO);
                    int total = 0;
                    if(!StringUtils.isEmpty(bizKeys) && bizKeys.size()>0) {
                        for (String bizKey : bizKeys) {
                            if (!StringUtils.isEmpty(avMap.get(bizKey))) {
                                total++;
                            }
                        }
                    }
                    if (total > 0) {
                        auditMap = new HashMap<>(2);
                        auditMap.put("businessType", map.get("menuName"));
                        auditMap.put("dataCount", total);
                        auditMap.put("guid", map.get("guid"));
                        result.add(auditMap);
                    }
                }
                //生成工资数据
                else if ("salaryGenerate".equals(map.get("menuIden"))) {
                    WfActionDTO wfActionDTO = new WfActionDTO();
                    // 菜单id
                    wfActionDTO.setMenuguid(map.get("guid").toString());
                    wfActionDTO.setStatus(StatusCodeEnum.TODO.getCode());
                    List<String> bizKeys = iSalaryWorkFlowService.queryWorkflowInfo(userDTO, wfActionDTO);
                    int total = 0;
                    if (!StringUtils.isEmpty(bizKeys) && bizKeys.size() > 0) {
                        for (String bizKey : bizKeys) {
                            if (!StringUtils.isEmpty(avMap.get(bizKey))) {
                                total++;
                            }
                        }
                    }

                    if (total > 0) {
                        auditMap = new HashMap<>(2);
                        auditMap.put("businessType", map.get("menuName"));
                        auditMap.put("dataCount", total);
                        auditMap.put("guid", map.get("guid"));
                        result.add(auditMap);
                    }
                }//生成银行数据
                else if ("bankCreate".equals(map.get("menuIden"))) {
                    // 日志类型 bankback 生成银行回盘,payback支付回盘，bankerror生成银行错误回盘
                    List<SalaryBackLog> bankback = salaryBackLogService.getSalaryBackLogByProvince(userDTO.getProvince(), "payback");
                    if (!CollectionUtils.isEmpty(bankback) && bankback.size() > 0) {
                        for (SalaryBackLog back : bankback) {
                            auditMap = new HashMap<>(2);
                            auditMap.put("businessType", "支付系统已支付！可以生产银行数据");
                            auditMap.put("dataCount", 1);
                            auditMap.put("guid", map.get("guid"));
                            auditMap.put("dataId", back.getId());
                            result.add(auditMap);
                        }
                    }
                    List<SalaryBackLog> bankerror = salaryBackLogService.getSalaryBackLogByProvince(userDTO.getProvince(), "bankerror");
                    if (!CollectionUtils.isEmpty(bankerror) && bankerror.size() > 0) {
                        for (SalaryBackLog back : bankerror) {
                            auditMap = new HashMap<>(2);
                            auditMap.put("businessType", back.getBank_name() + "未成功接收文件！请重新发送报盘文件！");
                            auditMap.put("dataCount", 1);
                            auditMap.put("guid", "");
                            auditMap.put("dataId", back.getId());
                            result.add(auditMap);
                        }
                    }
                    // 待办生成凭证提示
                    List<SalaryBackLog> bankBack = salaryBackLogService.getSalaryBackLogByProvince(userDTO.getProvince(), "paymentSuccess");
                    if (!CollectionUtils.isEmpty(bankBack) && bankback.size() > 0) {
                        for (SalaryBackLog back : bankBack) {
                            auditMap = new HashMap<>(2);
                            auditMap.put("businessType", back.getBank_name() + "凭证已生成!");
                            auditMap.put("dataCount", 1);
                            auditMap.put("guid", map.get("guid"));
                            auditMap.put("dataId", back.getId());
                            result.add(auditMap);
                        }
                    }
                }
                //异常卡号分发
                else if ("bankBack".equals(map.get("menuIden"))) {
                    // 日志类型 bankback 生成银行回盘,payback支付回盘，bankerror生成银行错误回盘，bankbacknof银行全部成功回盘
                    List<SalaryBackLog> bankback = salaryBackLogService.getSalaryBackLogByProvince(userDTO.getProvince(), "bankback");
                    if (!CollectionUtils.isEmpty(bankback) && bankback.size() > 0) {
                        for (SalaryBackLog back : bankback) {
                            auditMap = new HashMap<>(2);
                            auditMap.put("businessType", back.getBank_name() + "有回盘信息！有异常信息人员待分发！请查看！");
                            auditMap.put("dataCount", 1);
                            auditMap.put("guid", map.get("guid"));
                            auditMap.put("dataId", back.getId());
                            result.add(auditMap);
                        }
                    }
                    List<SalaryBackLog> bankbacknof = salaryBackLogService.getSalaryBackLogByProvince(userDTO.getProvince(), "bankbacknof");
                    if (!CollectionUtils.isEmpty(bankbacknof) && bankbacknof.size() > 0) {
                        for (SalaryBackLog back : bankbacknof) {
                            auditMap = new HashMap<>(2);
                            auditMap.put("businessType", back.getBank_name() + "有回盘信息！该盘所有人员代发成功！");
                            auditMap.put("dataCount", 1);
                            auditMap.put("guid","");
                            auditMap.put("dataId", back.getId());
                            result.add(auditMap);
                        }
                    }
                    List<SalaryBackLog> bankerror = salaryBackLogService.getSalaryBackLogByProvince(userDTO.getProvince(), "bankbackerror");
                    if (!CollectionUtils.isEmpty(bankerror) && bankerror.size() > 0) {
                        for (SalaryBackLog back : bankerror) {
                            auditMap = new HashMap<>(2);
                            auditMap.put("businessType", back.getBank_name() + "未成功接收文件！请重新发送报盘文件！");
                            auditMap.put("dataCount", 1);
                            auditMap.put("guid", "");
                            auditMap.put("dataId", back.getId());
                            result.add(auditMap);
                        }
                    }
                    //待办工资财政异常信息审核
                } else if ("expCardNoSub".equals(map.get("menuIden"))) {
                    //查询 所有状态为  bank_back_code='F' verified_in_agency_code=null的
//                   //查询银行回盘状态满足状态的异常卡号上报财政
                    List<SalaryBankPayBack> expCardNoSub = iSalaryBankPayBackDao.expCardNoSub();
                    //通过银行支付回盘表传回来的数据去查人员基本信息上报表
                    List<Map<String, Object>> agencyId = personExtSubmitDao.UnitOfAbnormal(expCardNoSub);
                    if (!CollectionUtils.isEmpty(agencyId) && agencyId.size() > 0) {
                        //查到人员基本信息表 去循环
                        for (Map<String, Object> personExt : agencyId) {
                            for (String agencyInfo : agencyIds) {
                                int total = agencyId.size();
                                //查到人员基本信息和单位信息去对比 看是否有权限
                                if (agencyInfo.equals(personExt.get("agency_id"))) {
                                    if (total > 0) {
                                        auditMap = new HashMap<>(2);
                                        auditMap.put("businessType",agencyMap.get(personExt.get("agency_id"))+map.get("menuName"));
                                        auditMap.put("dataCount",personExt.get("cn"));
                                        auditMap.put("guid", map.get("guid"));
                                        result.add(auditMap);
                                    }
                                    break;
                                }
                            }
                        }
                    }
                    //待办单位异常信息修改
              }  else if("Abnormal".equals(map.get("menuIden"))){
                    //查询银行回盘状态满足状态的待办单位修改卡号
                    List<SalaryBankPayBack> Abnormal = iSalaryBankPayBackDao.Abnormal();
                    //通过 满足状态的 去 sal_person_ext_submit表中查找 agencyId 以及数量
                    List<Map<String, Object>> agencyId = personExtSubmitDao.UnitOfAbnormal(Abnormal);
                    if (!CollectionUtils.isEmpty(agencyId) && agencyId.size() > 0) {
                        //查到人员基本信息表 去循环
                        for (Map<String, Object> personExt : agencyId) {
                            for (String agencyInfo : agencyIds) {
                                int total = agencyId.size();
                                //查到人员基本信息和单位信息去对比 看是否有权限
                                if (agencyInfo.equals(personExt.get("agency_id"))) {
                                    if (total > 0) {
                                        auditMap = new HashMap<>(2);

                                        auditMap.put("businessType",agencyMap.get(personExt.get("agency_id"))+"异常卡号待修改");
                                        auditMap.put("dataCount",personExt.get("cn"));
                                        auditMap.put("guid", map.get("guid"));
                                        result.add(auditMap);
                                    }
                                    break;
                                }
                            }
                        }
                    }
               }

            }
        }
        return result;
    }

    @Override
    public String getSalaryMonth(UserDTO userDTO) {
        //        查询当前月份
        return iAgencyVerifyDao.getSalaryMonth(userDTO.getProvince(), null);
    }

    @Override
    public void updateSalaryBackLog(String id) {
        salaryBackLogService.updateSalaryBackLog(id);
    }
}
